function [lpostd,likel,lpriord,lpriord_ss,flag_ok]=...
lmjPosteriorGeneral(parEstimated,parStru,priorStru,dataStru,model,posStru,flags)
% ======================================================================
% lmjPosteriorGeneral.M
% 
% Inputs 
% parEstimated:           vector Estimated Parameter 
% parStru:          .vector  Parameter Structure 

%                   either parStru.vec  Vector 
%                   or 
%                   parStru.namesEst    Name with the fields corresponding
%                   to parEstimated 
% model 
%   .handle 
%   .addsol 
%   .solveOptions 
% priorStru 
%   param.
%   steady. 

% follow the structure of GENSYSPOST_VEC by Alejandro Justiniano (C)
% IN addition to putting priors on parameters, our current model put
% priors on steady state values.
% 
% AJ June 2010. Allow for mixed frequency if ADDSOL.FLAG_MIXFREQ==0 
% Solution depends on whether G is fixed or time varying, as in the case of
% a cummulator. Determined by ADDSOL.FLAG_GTV ==0 or GTV == 1 
% =======================================================================
lpostd=-1e15;
likel=-inf;
lpriord=-inf;
lpriord_ss=-inf; 
flag_ok=0;
%% Keep this always to ensure working with latest Value
if flags.paramStructure==false 
    parStru.vector(posStru.param.estimated)=parEstimated;
    param=parStru.vector; 
end 
[likel,ssvec,flagLikelOK]=feval(addsol.funcLikel,parEstimated,param,posStru.param.estimated,model.handle,Y,trainvec,model.solveOptions,addsol);
%% Likelihood
if flagLikelOK~=1; return; end
%% Prior Density of Parameters
lpriord=sum(feval(@priordens,parvec,priorStru.param.prior,priorStru.param.alphap,priorStru.param.betap, priorStru.param.lbnd,priorStru.param.ubnd));
if isinf(abs(lpriord))==1;return;end
%% Prior Density on Steady State
if ~isempty(posStru.steady.estimated)
    lpriord_ss = sum(feval(@priordens,ssvec(posStru.steady.estimated), priorStru.steady.prior, priorStru.steady.alphap, priorStru.steady.betap, priorStru.steady.lbnd, priorStru.steady.ubnd));
    if isinf(abs(lpriord_ss))==1;return;end
else
    lpriord_ss=0;
end
%% log likelihood
lpostd = lpriord + lpriord_ss + likel;
%dispaj(lpostd); 
flag_ok=1;